/*
* @Author: Adolphus.Yao
* @Date:   2019-07-23 17:17:49
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2019-07-23 17:29:18
*/
package main

import (
    "fmt"
    "strings"
    "encoding/json"
    "strconv"
    "yss.dgd/pb/service/plstore"
)

func main() {
    lineNo := 0
    line := `{"level":"[32;1minfo[0m","app":"cardservice","ts":1571903990.0130978,"goid":"(8256,469377)","time":"2019-10-24T15:59:50.013+0800","hint":"smx/eX4Iob4u","uid":"5b63b0fa1e6cc904961c8c81","caller":"cardservicelogic/CardServiceHandler.go:316","msg":"[yao]3 update card list number! card_type: 5, card_index: 2, before: [{\"card_type\":1,\"card_number\":\"440782198802093933\",\"city_code\":\"440700\",\"card_use_count\":5,\"last_use_time_sec\":1562583747,\"card_index\":1},{\"card_type\":5,\"card_number\":\"4******************0\",\"city_code\":\"4407\",\"card_use_count\":4,\"last_use_time_sec\":1568680603,\"card_index\":2},{\"card_type\":3,\"card_number\":\"4****************3\",\"card_use_count\":5,\"last_use_time_sec\":1551364855,\"card_index\":3},{\"card_type\":6,\"card_number\":\"C70244248\",\"last_use_time_sec\":1533260326,\"card_index\":4},{\"card_type\":8,\"card_number\":\"EB2614162\",\"card_use_count\":1,\"last_use_time_sec\":1533458713,\"card_index\":5}], after: [{\"card_type\":1,\"card_number\":\"440782198802093933\",\"city_code\":\"440700\",\"card_use_count\":5,\"last_use_time_sec\":1562583747,\"card_index\":1},{\"card_type\":5,\"card_number\":\"44078219880209393300\",\"city_code\":\"4407\",\"card_use_count\":4,\"last_use_time_sec\":1568680603,\"card_index\":2},{\"card_type\":3,\"card_number\":\"4****************3\",\"card_use_count\":5,\"last_use_time_sec\":1551364855,\"card_index\":3},{\"card_type\":6,\"card_number\":\"C70244248\",\"last_use_time_sec\":1533260326,\"card_index\":4},{\"card_type\":8,\"card_number\":\"EB2614162\",\"card_use_count\":1,\"last_use_time_sec\":1533458713,\"card_index\":5}]\n"}`
    lineLen := len(line)
    if 0 < lineLen && strings.Contains(line, "card_type: 5,") {
        iIndex := strings.Index(line, "card_index:")
        jIndex := strings.Index(line, ", before:")
        if 0 < iIndex && 0 < jIndex && iIndex < jIndex {
            ls := line[iIndex:jIndex]
            cardIndexStr := strings.Replace(ls, "card_index: ", "", -1)
            cardIndex, err := strconv.Atoi(cardIndexStr)
            if nil != err {
                fmt.Println(err, "        ", cardIndexStr, " cardIndexStr err!!!!!!!!!! ", lineNo)
            }
            iIndex1 := strings.Index(line, "before:")
            jIndex1 := strings.Index(line, ", after:")
            ls1 := line[iIndex1:jIndex1]
            before := strings.Replace(ls1, "before: ", "", -1)
            before = strings.Replace(before, `\"`, `"`, -1)
            var cardInfoList []*plstore.CardInfo
            if err := json.Unmarshal([]byte(before), &cardInfoList); err != nil {
                fmt.Println(err, "        ", before, " err!!!!!!!!!! ", lineNo)
                return
            }
            for _, cardInfo := range cardInfoList {
                if uint32(cardIndex) == cardInfo.CardIndex {
                    fmt.Println(cardInfo.CityCode)
                }
            }
        } else {
            fmt.Println("Error line can not find json char!", line, " ", lineNo)
        }
    } else {
        fmt.Println("Error line is invaild data!", line, " ", lineNo)
    }
}
